/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.modular.xygenealogycate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.xygenealogycate.entity.XyGenealogyCate;
import vip.xiaonuo.modular.xygenealogycate.enums.XyGenealogyCateExceptionEnum;
import vip.xiaonuo.modular.xygenealogycate.mapper.XyGenealogyCateMapper;
import vip.xiaonuo.modular.xygenealogycate.param.XyGenealogyCateParam;
import vip.xiaonuo.modular.xygenealogycate.service.XyGenealogyCateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 谱文分类service接口实现类
 *
 * @author yanwei
 * @date 2023-05-05 15:44:29
 */
@Service
public class XyGenealogyCateServiceImpl extends ServiceImpl<XyGenealogyCateMapper, XyGenealogyCate> implements XyGenealogyCateService {

    @Override
    public PageResult<XyGenealogyCate> page(XyGenealogyCateParam xyGenealogyCateParam) {
        QueryWrapper<XyGenealogyCate> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(xyGenealogyCateParam)) {

            // 根据分类名称 查询
            if (ObjectUtil.isNotEmpty(xyGenealogyCateParam.getCateName())) {
                queryWrapper.lambda().eq(XyGenealogyCate::getCateName, xyGenealogyCateParam.getCateName());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<XyGenealogyCate> list(XyGenealogyCateParam xyGenealogyCateParam) {
        return this.list();
    }

    @Override
    public void add(XyGenealogyCateParam xyGenealogyCateParam) {
        XyGenealogyCate xyGenealogyCate = new XyGenealogyCate();
        BeanUtil.copyProperties(xyGenealogyCateParam, xyGenealogyCate);
        this.save(xyGenealogyCate);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<XyGenealogyCateParam> xyGenealogyCateParamList) {
        xyGenealogyCateParamList.forEach(xyGenealogyCateParam -> {
            this.removeById(xyGenealogyCateParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(XyGenealogyCateParam xyGenealogyCateParam) {
        XyGenealogyCate xyGenealogyCate = this.queryXyGenealogyCate(xyGenealogyCateParam);
        BeanUtil.copyProperties(xyGenealogyCateParam, xyGenealogyCate);
        this.updateById(xyGenealogyCate);
    }

    @Override
    public XyGenealogyCate detail(XyGenealogyCateParam xyGenealogyCateParam) {
        return this.queryXyGenealogyCate(xyGenealogyCateParam);
    }

    /**
     * 获取谱文分类
     *
     * @author yanwei
     * @date 2023-05-05 15:44:29
     */
    private XyGenealogyCate queryXyGenealogyCate(XyGenealogyCateParam xyGenealogyCateParam) {
        XyGenealogyCate xyGenealogyCate = this.getById(xyGenealogyCateParam.getId());
        if (ObjectUtil.isNull(xyGenealogyCate)) {
            throw new ServiceException(XyGenealogyCateExceptionEnum.NOT_EXIST);
        }
        return xyGenealogyCate;
    }

    @Override
    public void export(XyGenealogyCateParam xyGenealogyCateParam) {
        List<XyGenealogyCate> list = this.list(xyGenealogyCateParam);
        PoiUtil.exportExcelWithStream("SnowyXyGenealogyCate.xls", XyGenealogyCate.class, list);
    }

}
